/*
 * input.h
 *
 * Created: 6/17/2013 11:06:49 PM
 *  Author: Tim
 */ 


#ifndef INPUT_H_
#define INPUT_H_

#include <avr/io.h> 
#include <stdbool.h>

#include "../packets/status.h"
#include "../board/board.h"
#include "../features/stateMachine.h"
#include "../packets/structs/msgHeaderDefs.h"
#include "../packets/handlers/genPacketHandler.h"
#include "../packets//handlers/adcPacketHandler.h"
#include "../packets//handlers/dacPacketHandler.h"
#include "../packets//handlers/dioPacketHandler.h"
#include "../packets/handlers/encPacketHandler.h"


static inline uint8_t generalMsgHandler(uint8_t *packet)
{
	switch(packet[1])
	{
		case xtrg_mh:
			if(packet[2] == flashAdcInfo_mh)			daqAlloc.standAlone.recInfoAck = 1;
			else if(packet[2] == flashAdcData0_mh)		daqAlloc.standAlone.recDataAck = 1;
			else if(packet[2] == flashAdcData1_mh)		daqAlloc.standAlone.recDataAck = 1;
			else if(packet[2] == flashAdcData2_mh)		daqAlloc.standAlone.recDataAck = 1;
			else if(packet[2] == flashAdcData3_mh)		daqAlloc.standAlone.recDataAck = 1;
			else if(packet[2] == daqEndOfProgram_mh)	daqAlloc.standAlone.recEOPACK = 1;
		break;
		case resetDaq_mh:
		//sendGenAck(resetDaq_mh,1);
		resetDaq();
		break;
		case programStart_mh:
		startProgram();
		sendGenAck(programStart_mh,1);
		break;
		case programStop_mh:
		stopProgram();
		sendGenAck(programStop_mh,1);
		break;
		case daqStandAloneEn_mh:
		standAloneEnable();
		sendGenAck(daqStandAloneEn_mh,1);
		break;
	}
	return 0;
}

static inline uint8_t dacMsgHandler(uint8_t *packet)
{
	switch(packet[1])
	{
		case dacInitWaveform_mh:
		if (dacInitWaveformHandler((uint8_t *)packet))
		{
			sendGenAck(dacInitWaveform_mh,1);
			return 1;
		}
		else
		{
			sendGenAck(dacInitWaveform_mh,0);
			return 0;
		}
		break;
		case dacUpdate_mh:
		if (dacUpdateHandler((uint8_t *)packet))
		{
			sendGenAck(dacUpdate_mh,1);
			return 1;
		}
		else
		{
			sendGenAck(dacUpdate_mh,0);
			return 0;
		}
		break;
	}
}

static inline uint8_t dioMsgHandler(uint8_t *packet)
{
	switch(packet[1])
	{
	case dioInitPolling_mh:
		if(dioInitPollingHandler((uint8_t *)packet))
		{
			sendGenAck(dioInitPolling_mh,1);
			return 1;
		}
		else
		{
			sendGenAck(dioInitPolling_mh,0);
			return 0;
		}
	break;
	case dioOutSet_mh:
		dioOutSetHandler((uint8_t *)packet);
		sendGenAck(dioOutSet_mh,1);
		return 1;
	break;
	case dioOutClr_mh:
		dioOutClrHandler((uint8_t *)packet);
		sendGenAck(dioOutClr_mh,1);
		return 1;
	break;
	case dioDirSet_mh:
		dioDirSetHandler((uint8_t *)packet);
		sendGenAck(dioDirSet_mh,1);
		return 1;
	break;
	case dioDirClr_mh:
		dioDirClrHandler((uint8_t *)packet);
		sendGenAck(dioDirClr_mh,1);
		return 1;
	break;
	}
	return 0;
	
}

static inline uint8_t encMsgHandler(uint8_t *packet)
{
	if(encInitAsyncHandler((uint8_t *)packet))
	{
		sendGenAck(encInitAsync_mh,1);
		return 1;
	}
	else
	{
		sendGenAck(encInitAsync_mh,0);
		return 0;
	}
}

static inline uint8_t adcMsgHandler(uint8_t *packet)
{
	switch(packet[1])
	{		
		case adcInitAsync_mh:
			
			if(adcInitAsyncHandler((uint8_t *)packet))
			{
				sendGenAck(adcInitAsync_mh,1);
				return 1;
			}
			else
			{
				sendGenAck(adcInitAsync_mh,0);
				return 0;
			}
		break;
		case adcFlashInit_mh:
		
			if(adcFlashInit((uint8_t *)packet))
			{
				sendGenAck(adcFlashInit_mh,1);
				return 1;
			}	
			else
			{
				sendGenAck(adcFlashInit_mh,0);
				return 0;
			}
		break;	
		
	}
}


static inline uint8_t globalMsgHandler(uint8_t *packet)
{
	switch(packet[1] & 0xF0)
	{
		case general_mh_msk:
		if (generalMsgHandler((uint8_t *)packet))		return 1;
		else return 0;
		break;
		case adc_mh_msk:
		if (adcMsgHandler((uint8_t *)packet))			return 1;
		else return 0;
		break;
		case dac_mh_msk:
		if (dacMsgHandler((uint8_t *)packet))		return 1;
		else return 0;
		break;
		case dio_mh_msk:
		if (dioMsgHandler((uint8_t *)packet))		return 1;
		else return 0;
		break;
		case enc_mh_msk:
		if (encMsgHandler((uint8_t *)packet))		return 1;
		else return 0;
		break;
	}
}

#endif /* INPUT_H_ */